package com.github.postsanf.yinian.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.github.postsanf.yinian.BaseIntentService;
import com.github.postsanf.yinian.bean.YNUser;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.entity.YNCommonResponse;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import java.util.LinkedHashMap;

import easeui.utils.TextBase64Util;

public class RemoveMemberService extends BaseIntentService {
    public RemoveMemberService() {
        super("RemoveMemberService");
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        String userid = intent.getStringExtra("userid");
        String groupid = intent.getStringExtra("groupid");
        String owner = intent.getStringExtra("owner");
        String nickName = intent.getStringExtra("nickName");
        Log.d("yuhao", "userid="+userid);
        Log.d("yuhao", "groupid="+groupid);
        Log.d("yuhao", "owner="+owner);
        removeMember(userid,groupid,owner,nickName);
    }

    private void removeMember(final String mUserId, String mGroupId, String owner, final String nickName) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID,mUserId);
        mParam.put(CommonConstants.YNS_GROUP_ID,mGroupId);
        mParam.put(CommonConstants.YNS_OWNER,owner);
        StringRequest req = new StringRequest("https://api.zhuiyinanian.com/YinianProject/yinian/KickMembers")
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNCommonResponse resRemove = gson.fromJson(s, YNCommonResponse.class);
                        if (resRemove.getMsg().equals("success")){
                            showToast("移除成功");
                            sendBroadcast(new Intent(ACTIONs.remove));
                            String[] split = mUserId.split(",");
                            for (int i = 0; i < split.length; i++) {
                                sendBroadcast(new Intent(ACTIONs.deleteMember));
                            }
                            String[] split2 = nickName.split(",");
                            for (int i = 0; i <split2.length ; i++) {
                              //  sendExitHuanXinGroup(split2[i]);
                            }
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        Log.d("yuhao", "onFailure");
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    private void sendExitHuanXinGroup(String nickName) {
        EMMessage message = EMMessage.createTxtSendMessage(TextBase64Util.encode("移除"), application.getCurgimid());
        if (message == null) {
            return;
        }
        message.setChatType(EMMessage.ChatType.GroupChat);
        message.setAttribute("type", String.valueOf(66));
        message.setAttribute("intoQuitMessage", nickName + "被移除出了本圈子");
        EMClient.getInstance().chatManager().sendMessage(message);
    }
}
